2 * wiggle - apply rejected patches
4 * Copyright (C) 2003-2013 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program.
21 * Email: <neilb@suse.de>
25 * Split a stream into words or lines
28 * string of [A-Za-z0-9_]
30 * or single char (i.e. punctuation and newlines).
32 * A line is any string that ends with \n
34 * As a special case to allow proper aligning of multiple chunks
35 * in a patch, a word starting \0 will include 20+ chars with a newline
36 * second from the end.
38 * We make two passes through the stream.
39 * Firstly we count the number of item so an array can be allocated,
40 * then we store start and length of each item in the array
49 #include "ccan/hash/hash.h"
51 static int split_internal(char *start, char *end, int type,
61 if ((type & ByWord) && (type & IgnoreBlanks))
63 (*cp == ' ' || *cp == '\t')) {
69 if (*cp == '\0' && cp+19 < end) {
74 switch (type & ByMask) {
76 while (cp < end && *cp != '\n')
82 if (*cp == ' ' || *cp == '\t') {
88 } else if ((type & WholeWord) ||
89 isalnum(*cp) || *cp == '_') {
93 && (((type & WholeWord)
94 && *cp != ' ' && *cp != '\t'
103 if ((type & ByWord) && (type & IgnoreBlanks) &&
104 *start && *start != '\n')
106 (*cp2 == ' ' || *cp2 == '\t' || *cp2 == '\n')) {
113 list->len = cp-start;
114 list->plen = cp2-start;
115 list->prefix = prefix;
117 list->hash = hash(start, list->len, 0);
119 list->hash = atoi(start+1);
128 struct file split_stream(struct stream s, int type)
142 cnt = split_internal(c, end, type, NULL);
143 f.list = xmalloc(cnt*sizeof(struct elmnt));
145 f.elcnt = split_internal(c, end, type, f.list);